package com.hh.BacktrackQuestions;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author hanghao
 * @Date 2023/7/7 14:02
 * @Version 1.0
 * 组合总和 III
 * middle
 */

public class No216 {
    List<List<Integer>> res = new ArrayList<List<Integer>>();

    LinkedList<Integer> path = new LinkedList<Integer>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        int sum = 0;
        backTracking(k,n,0,1);
        return res;
    }

    private void backTracking(int k, int targetSum, int sum, int startIndex) {
        if(sum > targetSum) {
            return;
        }
        if(path.size() == k) {
            if(sum == targetSum) res.add(new ArrayList(path));
            return;
        }
        for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
            path.add(i);
            sum += i;
            backTracking(k, targetSum, sum,i + 1);
            path.removeLast();
            sum -= i;
        }
    }
}
